#pragma semicolon 1
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>

#define TEST_DEBUG 0
#define TEST_DEBUG_LOG 0

#define PLUGIN_VERSION    "1.0.1"

new Handle:cvarRangeSetting = INVALID_HANDLE;

public Plugin:myinfo = 
{
    name = "L4D2 Scavenge Gascan-Pipe Exploit Fix",
    author = "AtomicStryker",
    description = " Nukes Gascans a Player tries to move with explosives ",
    version = PLUGIN_VERSION,
    url = "http://forums.alliedmods.net/showthread.php?p=1081871"
}

public OnPluginStart()
{
    CreateConVar("l4d2_gascan_exploitfix_version", PLUGIN_VERSION, " Version of L4D2 Gas Can Pipe Bomb Exploit Fix Mod on this Server ", FCVAR_PLUGIN|FCVAR_SPONLY|FCVAR_NOTIFY|FCVAR_DONTRECORD);
    
    cvarRangeSetting = CreateConVar("l4d2_gascan_exploitfix_range", "400.0", " What range around a Pipe Bomb Explosion all Gascans must explode ", FCVAR_PLUGIN|FCVAR_NOTIFY);
    
    //HookEvent("weapon_fire", WeaponFire_Event); // was for testing purposes only
}

public OnEntityDestroyed(entity)
{
    if (!IsValidEdict(entity))
        return;
    
    decl String:classname[128];
    GetEdictClassname(entity, classname, sizeof(classname));
    if (!StrEqual(classname, "pipe_bomb_projectile", false))
        return;
    
    decl Float:Pos_Explosion[3], Float:Pos_Can[3];
    GetEntityAbsOrigin(entity, Pos_Explosion);
    
    DebugPrintToAll("Entity Destroyal of a Pipe Bomb, Entid %i, detected!", entity);
    DebugPrintToAll("X: %f, Y: %f, Z %f", Pos_Explosion[0], Pos_Explosion[1], Pos_Explosion[2]);

    new Float:rangesetting = GetConVarFloat(cvarRangeSetting);
    new maxentities = GetMaxEntities();
    
    for (new i = 1; i <= maxentities; i++)
    {
        if (!IsValidEntity(i) || !IsValidEdict(i)) continue; // exclude invalid entities.
        
        GetEdictClassname(i, classname, sizeof(classname));
        if (!StrEqual(classname, "weapon_gascan", false)) continue; // only the actual gas can entities are of concern
        
        GetEntityAbsOrigin(i, Pos_Can);
        if (GetVectorDistance(Pos_Can, Pos_Explosion) < rangesetting)
        {
            SetEntProp(i, Prop_Data, "m_iHealth", 5);
            AcceptEntityInput(i, "IgniteLifetime 1.0");
            AcceptEntityInput(i, "Ignite"); // ALL MUST BURN!!
            DebugPrintToAll("Detected nearby gascan, id %i, and ignited it", i);
        }
    }
}

// Downtowns faithful Debug Print
stock DebugPrintToAll(const String:format[], any:...)
{
    #if TEST_DEBUG	|| TEST_DEBUG_LOG
    decl String:buffer[192];
    
    VFormat(buffer, sizeof(buffer), format, 2);
    
    #if TEST_DEBUG
    PrintToChatAll("[TEST] %s", buffer);
    PrintToConsole(0, "[TEST] %s", buffer);
    #endif
    
    LogMessage("%s", buffer);
    #else
    //suppress "format" never used warning
    if(format[0])
        return;
    else
        return;
    #endif
}

// entity abs origin code from here
// http://forums.alliedmods.net/showPos_Explosiont.php?s=e5dce96f11b8e938274902a8ad8e75e9&p=885168&Pos_Explosiontcount=3
stock GetEntityAbsOrigin(entity,Float:origin[3])
{
    if (entity > 0 && IsValidEntity(entity))
    {
        decl Float:mins[3], Float:maxs[3];
        GetEntPropVector(entity,Prop_Send,"m_vecOrigin",origin);
        GetEntPropVector(entity,Prop_Send,"m_vecMins",mins);
        GetEntPropVector(entity,Prop_Send,"m_vecMaxs",maxs);
        
        origin[0] += (mins[0] + maxs[0]) * 0.5;
        origin[1] += (mins[1] + maxs[1]) * 0.5;
        origin[2] += (mins[2] + maxs[2]) * 0.5;
    }
}